import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/blind_box/blind_box_detail_page_controller.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/vo/address_list_vo.dart';
import 'package:xinxuan/vo/blindbox/blindbox_submit_order_vo.dart';
import 'package:xinxuan/vo/common_vo/common_address_item_vo.dart';

class BlindBoxConfirmOrderPageController extends GetxController {

  final BlindBoxDetailPageController blindBoxDetailPageController = Get.find();
  final CommonPageController commonPageController = Get.find();

  var addressItem = CommonAddressItem(
    id: 0,
    consignee: '',
    mobile: '',
    provinceStr: '',
    cityStr: '',
    districtStr: '}',
    address: '',
  ).obs;

  @override
  void onReady() {
    getAddressList();
    super.onReady();
  }

  /// 获取收货地址列表
  getAddressList() async {
    AddressListVo addressListVo = await HttpUtilService.getAddressList();
    if (addressListVo.data!.length > 0) {
      addressItem.value = addressListVo.data![0];
      update();
    }
  }

  /// 提交订单
  submitOrder(int id) async {
    if (addressItem.value.id == 0) {
      EasyLoading.showToast('请选择收货地址');
      return;
    }
    BlindBoxSubmitOrderVo blindBoxSubmitOrderVo = await HttpUtilService.submitBlindBoxOrder(id: id, addressId: addressItem.value.id!);
    commonPageController.getUserInfo(loading: false);
    Get.offAndToNamed(
      '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAY_PAGE}',
      arguments: {
        'payAmount': blindBoxSubmitOrderVo.data!.payableAmount!,
        'orderSn': blindBoxSubmitOrderVo.data!.orderSn
      }
    )!.then((value) {
      blindBoxDetailPageController.getBlindboxProductDetail();
    });
  }

}